//
// Created by 13562 on 2024/1/6.
//

#ifndef SRC_UTILS_H
#define SRC_UTILS_H

#include <iostream>
#include "rapidjson/document.h"


inline bool andGate(std::initializer_list<bool> statements) {
    for (bool statement : statements) {
        if (!statement) return false;
    }
    return true;
}


string getStringFrom(rapidjson::Value& dom, const std::string& key) {
	if (dom.HasMember(key.c_str())) {
		rapidjson::Value &node = dom[key.c_str()];
		if (node.IsString()) {
			return node.GetString();
		} else {
			std::cerr << "Type error in " << key << std::endl;
			return "";
		}
	} else {
		std::cerr << "Could not find a key named " << key << std::endl;
		return "";
	}
}


int getIntegerFrom(rapidjson::Value& dom, const std::string& key) {
	if (dom.HasMember(key.c_str())) {
		rapidjson::Value &node = dom[key.c_str()];
		if (node.IsInt()) {
			return node.GetInt();
		} else {
			std::cerr << "Type error in " << key << std::endl;
			return -1;
		}
	} else {
		std::cerr << "Could not find a key named " << key << std::endl;
		return -1;
	}
}


double getDoubleFrom(rapidjson::Value& dom, const std::string& key) {
	if (dom.HasMember(key.c_str())) {
		rapidjson::Value &node = dom[key.c_str()];
		if (node.IsDouble()) {
			return node.GetDouble();
		} else {
			std::cerr << "Type error in " << key << std::endl;
			return -1;
		}
	} else {
		std::cerr << "Could not find a key named " << key << std::endl;
		return -1;
	}
}


rapidjson::Value & getObjectFrom(rapidjson::Value & dom, const std::string& key) {
	if (dom.HasMember(key.c_str())) {
		rapidjson::Value &node = dom[key.c_str()];
		return node;
	} else {
		std::cerr << "Could not find a key named " << key << std::endl;
		return dom;
	}
}




#endif //SRC_UTILS_H
